package cn.edu.nchu.fragmentdemo.fragments;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;

import cn.edu.nchu.fragmentdemo.MainActivity;
import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.bean.Route;
import cn.edu.nchu.fragmentdemo.bean.RouteTask;
import cn.edu.nchu.fragmentdemo.bean.WayPoint;
import cn.edu.nchu.fragmentdemo.db.dao.RouteDao;
import cn.edu.nchu.fragmentdemo.db.dao.RouteTaskDao;
import cn.edu.nchu.fragmentdemo.db.server.RouteServer;
import lombok.SneakyThrows;


/**
 * @className: UavRouteSettingFragment
 * @description: TODO 类描述
 * @author: Kuangkeli
 **/
public class UavSettingBarFragment extends Fragment {


    private String TAG = "UavSettingBarFragment";

    private FragmentManager fragmentManager;
    private UavRouteSettingFragment uavRouteSettingFragment;
    private UavWayPointSettingFragment uavWayPointSettingFragment;
    private UavRouteTaskNameEditFragment uavRouteTaskNameEditFragment;
    private ImageButton homeBtn;
    private ImageButton routeSettingBtn;
    private ImageButton waypointSettingBtn;

    /**
     * 当前航线任务
     */
    private RouteTask currentRouteTask;

    /**
     * 当前航线
     */
    private Route currentRoute;

    /**
     * 航线任务Dao
     */
    Dao<RouteTask, Integer> routeTaskDao;

    /**
     * 航线任务Dao
     */
    Dao<Route, Integer> routeDao;

    /**
     * 当前选择的按钮
     */
    private ImageButton selectedButton;

    private Context context;

    private MainActivity mainActivity;

    Dao<WayPoint, Integer> wayPointsDao;

    int routeId;


    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_uav_setting_bar, container, false);
        return rootView;
    }

    @SneakyThrows
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        initClickListener();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        homeBtn = view.findViewById(R.id.home_btn);
        routeSettingBtn = view.findViewById(R.id.route_setting_btn);
        waypointSettingBtn = view.findViewById(R.id.waypoint_setting_btn);
    }

    public void init() throws SQLException {
        mainActivity = (MainActivity) MainActivity.getActivity();
        context = mainActivity.getApplicationContext();
        uavRouteSettingFragment = new UavRouteSettingFragment();
        uavWayPointSettingFragment = new UavWayPointSettingFragment();
        uavRouteTaskNameEditFragment = new UavRouteTaskNameEditFragment();
        currentRouteTask = new RouteTask();
        routeTaskDao = RouteTaskDao.getRouteTaskDao(requireContext());
        routeDao = RouteDao.getRouteDao(requireContext());
        //从bundle中读取左侧航线任务栏对应航线任务传递来的id
        Bundle bundle = getArguments();
        if (bundle != null) {
            int value = bundle.getInt("routeTaskId");
            //根据传来的id初始化当前航线航点设置的当前航线任务
            getRouteTaskById(value);
            //在这里实例航线对象 如果是空的就new

            if(RouteServer.getRouteIdByRouteTaskId(value,routeDao)!=null) {
                routeId = RouteServer.getRouteIdByRouteTaskId(value,routeDao);
            }
            else{
                routeId = newRoute();
            }
            bundle.putInt("routeId",routeId);
            uavRouteSettingFragment.setArguments(bundle);
            uavWayPointSettingFragment.setArguments(bundle);

            if (currentRouteTask != null) {
                bundle.putString("RouteTaskName", currentRouteTask.getTaskName());
                uavRouteTaskNameEditFragment.setArguments(bundle);
            }

        }

        /**
         * 设置最初页面为航线设置
         */
        selectedButton = homeBtn;
        selectButton(selectedButton);
        replaceRightFragment(uavRouteTaskNameEditFragment);
    }


    private Integer newRoute() throws SQLException {
        long maxId = routeDao.queryRawValue("SELECT MAX(id) FROM route");
        currentRoute = new Route((int)maxId+1,"",0,0,null,50.0,5.0,0,0,0,0.0,2.0,2.0,0.0,null,0,currentRouteTask);
        routeDao.create(currentRoute);
        return (int)maxId+1;
    }

    private void getRouteTaskById(final int routeTaskId) throws SQLException {
        currentRouteTask = routeTaskDao.queryForId(routeTaskId);
    }


    /**
     * 传入一个fragment用于替换右侧的占位视图
     *
     * @param fragment
     */
    public void replaceRightFragment(Fragment fragment) {
        /**
         * 对于支持库中的Fragment使用
         */
        fragmentManager = getFragmentManager();
        assert fragmentManager != null;
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragmentLayout_container_right, fragment);
        transaction.commit();
    }

    /**
     * 初始化点击事件监听器
     */
    private void initClickListener() {
        homeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectButton(homeBtn);
                replaceRightFragment(uavRouteTaskNameEditFragment);
            }
        });

        routeSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectButton(routeSettingBtn);
                replaceRightFragment(uavRouteSettingFragment);
            }
        });

        waypointSettingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectButton(waypointSettingBtn);
                replaceRightFragment(uavWayPointSettingFragment);
            }
        });


    }

    private void selectButton(ImageButton selectedButton) {

        if (this.selectedButton != null) {
            // 将上一个选中的按钮恢复为默认图片
            int defaultImageResId = getButtonDefaultImageResId(this.selectedButton);
            this.selectedButton.setImageResource(defaultImageResId);
        }

        // 设置当前选中按钮的图片
        int goingImageResId = getButtonGoingImageResId(selectedButton);
        selectedButton.setImageResource(goingImageResId);
        // 更新当前选中的按钮
        this.selectedButton = selectedButton;
    }

    private int getButtonDefaultImageResId(ImageButton button) {
        String buttonId = getResources().getResourceEntryName(button.getId());
        return getResources().getIdentifier(buttonId, "drawable", requireContext().getPackageName());
    }

    private int getButtonGoingImageResId(ImageButton button) {
        String buttonId = getResources().getResourceEntryName(button.getId());
        return getResources().getIdentifier(buttonId + "_going", "drawable", requireContext().getPackageName());
    }


    public String getTAG() {
        return TAG;
    }


}
